package com.example.kotlinproject.net

import android.content.Context
import com.example.kotlinproject.entity.LoginResponseWrapper
import com.example.kotlinproject.utils.LoadingDialog
import io.reactivex.Observer
import io.reactivex.disposables.Disposable


abstract class ApiResponse<T>(val context: Context): Observer<LoginResponseWrapper<T>> {

    private var isShow =true
    //次构造,次构造必须调用主构造
    constructor(context: Context,isShow:Boolean =false):this(context){
        this.isShow=isShow
    }

    abstract fun success(loginResponse: T?)
    abstract fun fail(errorMsg:String?)

    override fun onComplete() {
        LoadingDialog.cancel()
    }

    override fun onSubscribe(d: Disposable) {
        // 弹出 加载框
        if (isShow) {
            LoadingDialog.show(context)
        }
    }

    override fun onNext(t: LoginResponseWrapper<T>) {
       if(t.data==null){
           fail(t.errorMsg)
       }else{
           success(t.data)
       }
    }

    override fun onError(e: Throwable) {
        LoadingDialog.cancel()
        fail(e.message)
    }
}